﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class Grouping : DemoBaseUserControl
    {
        public Grouping()
        {
            InitializeComponent();
            txtGroupBarText.KeyUp += new KeyEventHandler(txtGroupBarText_KeyUp);
            txtGroupBarText.Leave += new EventHandler(txtGroupBarText_Leave);
            this.Title = "Outlook 样式分组";
            this.Description = "Spread .NET  提供 Outlook 样式分组功能。本例中拖动一个列头到分组条（表单上部空白区域）创建一个分组。再拖另外的列头可以创建嵌套的分组。把分组拖出分组条可以移除分组。分组也可以通过代码实现。" +
                               "点击创建分组按钮可以创建一个基于当前选择列的分组。点击移除分组的按钮可以移除所有分组。";
        }

        void txtGroupBarText_Leave(object sender, EventArgs e)
        {
            if (txtGroupBarText.Text != null)
            {
                FpSpread1.ActiveSheet.GroupBarInfo.Text = txtGroupBarText.Text;
            }
        }

        void txtGroupBarText_KeyUp(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Enter) & (txtGroupBarText.Text != null))
            {
                FpSpread1.ActiveSheet.GroupBarInfo.Text = txtGroupBarText.Text;
            }
        }

        private void radioButtonNotSet_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonNotSet.Checked == true)
            {
                radioButtonCollapseAll.Checked = false;
                radioButtonExpandAll.Checked = false;
                radioButtonExpandParent.Checked = false;
                FpSpread1.Sheets[0].GroupingPolicy = FarPoint.Win.Spread.Model.GroupingPolicy.NotSet;
            }
        }

        private void radioButtonExpandAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonExpandAll.Checked == true)
            {
                radioButtonCollapseAll.Checked = false;
                radioButtonNotSet.Checked = false;
                radioButtonExpandParent.Checked = false;
                FpSpread1.Sheets[0].GroupingPolicy = FarPoint.Win.Spread.Model.GroupingPolicy.ExpandAll;
            }
        }

        private void radioButtonCollapseAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonCollapseAll.Checked == true)
            {
                radioButtonExpandAll.Checked = false;
                radioButtonNotSet.Checked = false;
                radioButtonExpandParent.Checked = false;
                FpSpread1.Sheets[0].GroupingPolicy = FarPoint.Win.Spread.Model.GroupingPolicy.CollapseAll;
            }
        }

        private void radioButtonExpandParent_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonExpandParent.Checked == true)
            {
                radioButtonExpandAll.Checked = false;
                radioButtonNotSet.Checked = false;
                radioButtonCollapseAll.Checked = false;
                FpSpread1.Sheets[0].GroupingPolicy = FarPoint.Win.Spread.Model.GroupingPolicy.ExpandParents;
            }
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = FpSpread1.ActiveSheet.GroupBarInfo.BackColor;
            colorDialog1.ShowDialog();
            if (!colorDialog1.Color.Equals(FpSpread1.ActiveSheet.GroupBarInfo.BackColor))
            {
                FpSpread1.ActiveSheet.GroupBarInfo.BackColor = colorDialog1.Color;
                FpSpread1.Refresh();
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDown1.Value > 0)
            {
                FpSpread1.ActiveSheet.GroupBarInfo.Height = Convert.ToInt32(numericUpDown1.Value);
            }
        }

        private void btnForeColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = FpSpread1.ActiveSheet.GroupBarInfo.ForeColor;
            colorDialog1.ShowDialog();
            if (!colorDialog1.Color.Equals(FpSpread1.ActiveSheet.GroupBarInfo.ForeColor))
            {
                FpSpread1.ActiveSheet.GroupBarInfo.ForeColor = colorDialog1.Color;
                FpSpread1.Refresh();
            }
        }

        private void Grouping_Load_1(object sender, EventArgs e)
        {
            //this.FpSpread1_Sheet1.
            FpSpread1.DataSource = 销售明细表TableAdapter.GetData();
            FpSpread1.AllowColumnMove = true;
            FpSpread1.ScrollBarTrackPolicy = FarPoint.Win.Spread.ScrollBarTrackPolicy.Both;
            FpSpread1.Sheets[0].AllowGroup = true;
            FpSpread1.Sheets[0].GroupBarVisible = true;

            for (int intC = 0; intC < FpSpread1.Sheets[0].ColumnCount; intC++)
            {
                this.FpSpread1.Sheets[0].Columns[intC].Width = 100;
            }

            FpSpread1.Sheets[0].DefaultStyle.BackColor = Color.AliceBlue;
        }

        private void butRemove_Click(object sender, EventArgs e)
        {
            try
            {
                FpSpread1.Sheets[0].Models.Data = ((FarPoint.Win.Spread.Model.GroupDataModel)FpSpread1.Sheets[0].Models.Data).TargetModel;
            }
            catch (Exception) { }
            this.btnGroup.Enabled = true;
        }

        private void btnGroup_Click(object sender, EventArgs e)
        {
                FarPoint.Win.Spread.Model.GroupDataModel gm = new FarPoint.Win.Spread.Model.GroupDataModel(FpSpread1.Sheets[0].Models.Data);
                FarPoint.Win.Spread.SortInfo[] si = new FarPoint.Win.Spread.SortInfo[] { new FarPoint.Win.Spread.SortInfo(FpSpread1.Sheets[0].ActiveColumnIndex, true) };
                gm.Groups.Add(si);
                gm.Group(si);
                this.btnGroup.Enabled = false;
        }

        private void FpSpread1_Grouping(object sender, FarPoint.Win.Spread.GroupingEventArgs e)
        {
            this.btnGroup.Enabled = false;
        }
    }
}
